
package ${packageName!''}.controller;

import ${packageName!''}.dto.PageInfo;
import ${packageName!''}.dto.Pageable;
import ${packageName!''}.dto.ResultBean;
import ${packageName!''}.entity.${beanClassName!''};
import ${packageName!''}.exception.BusinessException;
import ${packageName!''}.service.${beanClassName!''}Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ${name!''}-Controller
 */
@Controller
@RequestMapping("${path!''}")
public class ${beanClassName!''}Controller {

    private static final Logger log = LoggerFactory.getLogger(${beanClassName!''}Controller.class);

    @Autowired
    ${beanClassName!''}Service ${beanName!''}Service;

    @RequestMapping("list.htm")
    public String toMain(){
        return "${path!''}/list";
    }
    @RequestMapping("add.htm")
    public String toAdd(){
        return "${path!''}/add";
    }
    @RequestMapping("update.htm")
    public String toUpdate(){
        return "${path!''}/update";
    }

    /**
     * 分页查询数据
     * @param pageable 查询参数封装类实体
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public PageInfo<${beanClassName!''}> list(Pageable pageable){
        PageInfo<${beanClassName!''}> page = ${beanName!''}Service.getPage(pageable);
        return page;
    }

    /**
     * 增加数据
     * @param ${beanName!''}
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public ResultBean add(${beanClassName!''} ${beanName!''}){
        ResultBean result = new ResultBean();
        try {
            if (${beanName!''}==null)
                result.setSuccess(false).setMsg("保存失败，保存信息为空！");
            else
                ${beanName!''}Service.save(${beanName!''});
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("保存数据异常",e);
        }
        return result;
    }
    /**
     * 更新数据
     * @param ${beanName!''} 业务实例，id属性不能为空
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultBean update(${beanClassName!''} ${beanName!''}){
        ResultBean result = new ResultBean();
        try {
            if (${beanName!''}==null || StringUtils.isEmpty(${beanName!''}.getId())){
                result.setSuccess(false).setMsg("请选择要更新的数据！");
            }else
                ${beanName!''}Service.update(${beanName!''});
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("更新数据异常",e);
        }
        return result;
    }

    /**
     * 单个删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultBean delete(@RequestParam(value = "id",required = true) Long id){
        ResultBean result = new ResultBean();
        try {
            if (StringUtils.isEmpty(id)){
                result.setSuccess(false).setMsg("请选择要删除的数据！");
            }else
                ${beanName!''}Service.delete(id);
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("删除数据异常",e);
        }
        return result;
    }

    /**
     * 批量删除，最多支持1000个一次
     * @param ids
     * @return
     */
    @RequestMapping("delete_batch")
    @ResponseBody
    public ResultBean deleteBatch(@RequestParam(value = "ids",required = true) Long[] ids){
        ResultBean result = new ResultBean();
        try {
            if (ids==null || ids.length==0){
                result.setSuccess(false).setMsg("请选择要删除的数据！");
            }else if(ids.length>1000){
                result.setSuccess(false).setMsg("一次最多删除500条记录！");
            }else{
                ${beanName!''}Service.deleteBatch(ids);
            }
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("批量删除数据异常",e);
        }
        return result;
    }
}
